package com.hktf.uilib.engine;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import com.hktf.corelib.utils.Config;
import com.hktf.corelib.utils.FileUtil;
import com.hktf.corelib.utils.MD5;

import java.io.File;

public class DownloadEngine {

    public static void doloadFile(String url,String fileName,String md5){
        File file = new File(Config.getVoice());
        if (!file.exists()) {
            file.mkdir();
        }
        String path=Config.getVoice()+fileName;
        OkGo.<File>get(url).execute(new FileCallback(Config.getVoice(), fileName) {

            @Override
            public void onSuccess(Response<File> response) {
                File body = response.body();
                //检查md5
                try {
                    String fileMd5 = MD5.getHash(path, "md5");
                    if (!fileMd5.equalsIgnoreCase(md5)) {

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Response<File> response) {
                super.onError(response);
                FileUtil.deleteFile(path);
            }

            @Override
            public void downloadProgress(Progress progress) {
                super.downloadProgress(progress);
            }
        });
    }

    public static void doloadVoiceFile(String url,FileCallback fileCallback){
        File file = new File(Config.getVoice());
        if (!file.exists()) {
            file.mkdir();
        }
        OkGo.<File>get(url).execute(fileCallback);
    }
}
